import { getToken } from '@/utils/auth';
import { queryGuestTheme, queryUserTheme } from '@/api/system/theme/user';
const theme = {
  state: {
    themes: {}
  },
  mutations: {
    SET_THEME: (state, themes) => {
      state.themes = themes;
      // console.log("当前主题", themes.id);
      const themesJson = JSON.stringify(themes);
      localStorage.setItem('themes', themes.id);
    }
  },
  actions: {
    GetThemes({ commit }) {
      return new Promise((resolve, reject) => {
        if (getToken()) {
          queryUserTheme()
            .then(res => {
              const { data } = res;
              commit('SET_THEME', data.data);
              resolve({ data: data.data });
            })
            .catch(() => {
              reject({ data: '获取主题信息失败' });
            });
        } else {
          queryGuestTheme()
            .then(res => {
              const { data } = res;
              commit('SET_THEME', data.data);
              resolve({ data: data.data });
            })
            .catch(() => {
              reject({ data: '获取主题信息失败' });
            });
        }
      }).catch(e => {});
    }
  }
};
export default theme;
